////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.orders.windows {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import kiwi.utils.Validation;
	
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.display.windows.BaseWindow;
	import plantool.events.OrderEvent;
	import plantool.events.OrderrowEvent;
	import plantool.modules.orders.data.Order;
	import plantool.modules.orders.windows.objects.OrderListWindowOrderRow;
	
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class OrderListWindow extends BaseWindow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private static var _instance:OrderListWindow		= null;
		private var _isUpdating:Boolean 	= false;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function OrderListWindow(pvt:PrivateConstructorClass) {
			super(false);
		}
		
		public override function dispose():void {
			//remove eventlisteners
			orderApi.removeEventListener(OrderrowEvent.ORDERROWPLANNING_CREATED	, onPlanningRows_updated);
			
			//remove instance
			_instance = null;
			
			//dispose
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		public static function getInstance():OrderListWindow {
			//check if instance exists, if not create one
			if (!Validation.isSet(_instance)) {
				_instance = new OrderListWindow(new PrivateConstructorClass());
			}
			
			//return instance
			return _instance;
		}
		
		private function onError_query(result:QueryErrorResult):void {
			trace("Error in query: ", result.query);
			trace(" - ", result.message);
		}
		
		private function createRows(order:Order):void {
			//create row for each orderrow
			for (var i:uint = 0; i < order.orderrows.length; i++) {
				var row:OrderListWindowOrderRow = new OrderListWindowOrderRow(this);
				row.order						= order;
				row.orderRow					= order.orderrows[i];
				
				//position the row
				row.y 		= numRows * 20;
				row.width 	= width;
				
				//add the row
				addChild(row);
			}
		}
		
		private function onOrder_rowsLoaded(e:OrderEvent):void {
			//get order
			var order:Order = e.currentTarget as Order;
			
			//remove event
			order.removeEventListener(OrderEvent.ROWS_LOADED, onOrder_rowsLoaded);

			//create rows
			createRows(order);
		}
		
		private function onComplete_getOrders(result:QueryResult):void {
			//show users
			if (Validation.isSet(result.data) && result.data.length > 0) {
				//create a row for each user
				for (var i:uint = 0; i < result.data.length; i++) {
					//get order
					var order:Order = result.data[i];
					
					//check if rows are loaded
					if (order.rowsLoaded) {
						createRows(order);
					}
					//else listen for even
					else {
						order.addEventListener(OrderEvent.ROWS_LOADED		, onOrder_rowsLoaded);
					}
				}
			}
			
			//unset updating
			_isUpdating = false;
		}
		
		private function onPlanningRows_updated(e:OrderrowEvent):void {
			if (!_isUpdating) {
				//set updating
				_isUpdating = true;
				
				//clear all rows
				clearContent();
				
				//load new users
				orderApi.getOrders(onComplete_getOrders, onError_query);
			}
		}
		
		protected override function init():void {
			//call super init
			super.init();
			
			//set title
			title = "Orders";
			
			//go through all users and create rows for them
			orderApi.addEventListener(OrderrowEvent.ORDERROWPLANNING_CREATED	, onPlanningRows_updated);
			onPlanningRows_updated(null);
		}
	}
}

//--------------------------------------------------------------------------
//END OF PACKAGE
//Define the PrivateConstructorClass here so an instance cannot be by anyone
//except for the class defined in the above package
//--------------------------------------------------------------------------
class PrivateConstructorClass {
	//--------------------------------------------------------------------------
	//Constructor and Destructor
	//--------------------------------------------------------------------------
	public function PrivateConstructorClass() {
	}
}